<!-- 订单数据上传 -->
<template>
  <el-button @click="handleUpload" :loading="btnLoading" :type="type">
    {{title}}
    <input @change="handleChangeFile" type="file" ref="filElem" :accept="accept" class="file-hidden" style="position: absolute;width: 0;height: 0;z-index: -1;" />
    <el-dialog :title="dialogTitle" :visible.sync="show" width="600px" append-to-body @click.stop>
      <el-table :data="tableList" ref="tableData" border fit>
        <el-table-column type="index" label="序号" align="center" width="70"></el-table-column>
        <el-table-column prop="orderSn" label="订单编号" align="center"></el-table-column>
        <el-table-column prop="note" label="失败原因" align="center"></el-table-column>
      </el-table>
    </el-dialog>
  </el-button>

</template>

<script>
import { OrderInfoImportExcel } from '@/api/order'
export default {
  name: '',
  components: {},
  data () {
    return {
      btnLoading: false,
      show: false,
      dialogTitle: '失败数据',
      tableList: []
    }
  },
  props: {
    accept: {
      type: String,
      default: '.csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    },
    title: {
      type: String,
      default: '订单数据上传'
    },
    type: {
      type: String,
      default: 'primary'
    }
  },
  watch: {},
  computed: {},
  mounted () { },
  methods: {
    handleUpload () {
      this.$refs.filElem.dispatchEvent(new MouseEvent('click'))
    },
    handleChangeFile (file) {
      let files = file.target.files[0] || null
      this.uploadFile(files)
    },
    // 获取oss签名
    uploadFile (file) {
      this.btnLoading = true
      let ossFormData = new FormData()
      ossFormData.append('file', file)
      OrderInfoImportExcel(ossFormData).then(res => {
        this.btnLoading = false
        this.$refs.filElem.value = ''
        const data = res.data
        if (data.code === 200 || data.code === '200') {
          this.$message.success(res.message || '上传成功')
        } else {
          this.tableList = data.data || []
          if (this.tableList.length >= 1) {
            this.show = true
          }
          this.$message.error(data.message || '上传文件失败！')
        }
        this.$emit('getUploadResult', true)
      }).catch(e => {
        this.btnLoading = false
        this.$refs.filElem.value = ''
        this.$message.error('上传文件失败！')
      })
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
